<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="合同名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="合同名称"></el-input>
    </el-form-item>
    <el-form-item label="合同编号" prop="num">
      <el-input v-model="dataForm.num" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="有效时间" prop="effectiveTime">
      <el-input v-model="dataForm.effectiveTime" placeholder="有效时间"></el-input>
    </el-form-item>
    <el-form-item label="合同金额" prop="contractPrice">
      <el-input v-model="dataForm.contractPrice" placeholder="合同金额"></el-input>
    </el-form-item>
    <el-form-item label="合同状态   0：未激活   1： 已激活" prop="contractType">
      <el-input v-model="dataForm.contractType" placeholder="合同状态   0：未激活   1： 已激活"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          num: '',
          effectiveTime: '',
          contractPrice: '',
          contractType: ''
        },
        dataRule: {
          name: [
            { required: true, message: '合同名称不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          effectiveTime: [
            { required: true, message: '有效时间不能为空', trigger: 'blur' }
          ],
          contractPrice: [
            { required: true, message: '合同金额不能为空', trigger: 'blur' }
          ],
          contractType: [
            { required: true, message: '合同状态   0：未激活   1： 已激活不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          // console.log(this.dataForm.id)
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            console.log("修改合同的 id 是："+this.dataForm.id)

            axios.get(`http://localhost:10000/contract/contract/info/${this.dataForm.id}`)
              .then(({ data }) => {
                // data = data.data
                console.log("获取到的合同数据是：",data);

                  if (data && data.code === 0) {
                    this.dataForm.name = data.contract.name
                    this.dataForm.num = data.contract.num
                    this.dataForm.effectiveTime = data.contract.effectiveTime
                    this.dataForm.contractPrice = data.contract.contractPrice
                    this.dataForm.contractType = data.contract.contractType
                  }

                // if (data && data.code === 0) {
                //   this.dataList = data.page.list;
                //   console.log("获取到的数据列表",this.dataList);
                //   this.totalPage = data.page.totalCount;
                // } else {
                //   this.dataList = [];
                //   this.totalPage = 0;
                // }
                // this.dataListLoading = false;
                // console.log("数据填充结束");
              })
              .catch(function (error) {
                console.log(error);
              });

            // this.$http({
            //   url: this.$http.adornUrl(`/contract/contract/info/${this.dataForm.id}`),
            //   method: 'get',
            //   params: this.$http.adornParams()
            // }).then(({data}) => {
            //   if (data && data.code === 0) {
            //     this.dataForm.name = data.contract.name
            //     this.dataForm.num = data.contract.num
            //     this.dataForm.effectiveTime = data.contract.effectiveTime
            //     this.dataForm.contractPrice = data.contract.contractPrice
            //     this.dataForm.contractType = data.contract.contractType
            //   }
            // })
          }
        })
      },
      // 表单提交
      //  url: this.$http.adornUrl(`/contract/contract/${!this.dataForm.id ? 'save' : 'update'}`),
      dataFormSubmit () {
        axios.post(`http://localhost:10000/contract/contract/${!this.dataForm.id ? 'save' : 'update'}`, {
          'id': this.dataForm.id || undefined,
          'name': this.dataForm.name,
          'num': this.dataForm.num,
          'effectiveTime': this.dataForm.effectiveTime,
          'contractPrice': this.dataForm.contractPrice,
          'contractType': this.dataForm.contractType
        })
        .then(({ data }) => {
          // data = data.data
          console.log("保存的合同数据为：",data);
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                // this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        
        })
        .catch(function (error) {
          console.log(error);
        });

        // this.$refs['dataForm'].validate((valid) => {
        //   if (valid) {
        //     this.$http({
        //       url: this.$http.adornUrl(`/contract/contract/${!this.dataForm.id ? 'save' : 'update'}`),
        //       method: 'post',
        //       data: this.$http.adornData({
        //         'id': this.dataForm.id || undefined,
        //         'name': this.dataForm.name,
        //         'num': this.dataForm.num,
        //         'effectiveTime': this.dataForm.effectiveTime,
        //         'contractPrice': this.dataForm.contractPrice,
        //         'contractType': this.dataForm.contractType
        //       })
        //     }).then(({data}) => {
        //       if (data && data.code === 0) {
        //         this.$message({
        //           message: '操作成功',
        //           type: 'success',
        //           duration: 1500,
        //           onClose: () => {
        //             this.visible = false
        //             this.$emit('refreshDataList')
        //           }
        //         })
        //       } else {
        //         this.$message.error(data.msg)
        //       }
        //     })
        //   }
        // })
      }
    }
  }
</script>
